/***  DOCUMENTATION LAYER

	moshi myspace 95 thesis widget (m95.wx.js)

	Author: Tom at klenwell@gmail.com
	Last Updated: Nov 2006

	FUNCTIONS
		wx_m95()
		wx_m95_html(text, num)
		js_get_script_path(js_fname)
		js_rand(min, max)
		
  NOTES
		To link: <script type="text/javascript" src="path/to/m95.wx.js"></script>
		
		To display widget: 
			<div id="m95_wrap"><script type="text/javascript">wx_m95();</script></div>
		
		The following css settings may be used to modify appearance
		
		RECOMMENDED
    #m95_wrap { position:absolute; top:10px; right:10px; }
    .m95_wx
    {
     font-family:"Palatino Linotype", "Book Antiqua", Palatino, serif;
     font-size:14px;
     font-style:italic;
     font-weight:bold;
     color:#330000; 
    }
    .m95_wx a { color:#993300; text-decoration:none; }
    .m95_wx a:hover { color:#ff9900; }
    
    OPTIONAL
    .m95_mid {}
    .m95_text {}
    .m95_num {}
    .m95_top {}

		
	DEPENDENCIES
		requires three image files for background: m95_top.png, m95_mid.png, m95_btm.png		
		
	LICENSE
		GPL (see http://www.gnu.org/copyleft/gpl.html)
		This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, &c.

______________________________________________________________________________*/

// global parameters
var debug = 0;	// debugging on?
var list95_src_fname = 'm95.src.js' 	// thesis source file
var m95_css_fname = 'm95.css';
var js_fname = 'm95.wx.js';						// this file (for autostyling below)

// autostyle
if ( typeof m95_autostyle == 'undefined' ) m95_autostyle = 0;

// get script path
var dir_path = js_get_script_path(js_fname);

// background images
var bg_top_src = dir_path + 'm95_top.png';
var bg_mid_src = dir_path + 'm95_mid.png';
var bg_btm_src = dir_path + 'm95_btm.png';
var list95_src = dir_path + list95_src_fname;

// load thesis source file
document.write('<script type="text/javascript" src="' + list95_src + '"></script>');

// wx_myspace95thesis
/*____________________________________________________________________________*/
function wx_m95()
{
// *** DATA

	// hard parameters
	_debug = 0;

	
// *** MANIPULATE

	// sanity check
	if ( !LIST95.length )
	{
		if ( debug ) alert('95thesis array empty!');
		return;
	}
	
	// num items in list
	var len = LIST95.length;
	
	// get random number
	var idx = js_rand(0, len-1);
	
	// get thesis selection
	selection = LIST95[idx];
	
	// get number of entry
	num = ( idx == len-1 ) ? 95 : idx + 1;
	
	// build html for display
	m95_html = wx_m95_html(selection, num);
	
	// display
	document.write(m95_html);
	
	// return (void)
	return;	
	
} // end Fx
/*____________________________________________________________________________*/


// wx_myspace95thesis
/*____________________________________________________________________________*/
function wx_m95_html(text, num)
{
// *** DATA

	// elements
	var top_block = '';
	var num_block = '';
	var mid_block = '';
	var m95_footer = '';
	var yanker = '';
	
	// return
	var html = '';
	

// *** MANIPULATE

	// internal style settings
	top_block_style = 'height:34px; background-image:url(' + bg_top_src + '); background-repeat:no-repeat; background-position:top center;';
	mid_block_style = 'background-image:url(' + bg_mid_src + '); background-repeat:repeat-y; background-position:top center; padding:2px 20px;';
	wx_style = 'position:relative; width:180px; padding-bottom:24px; background-image:url(' + bg_btm_src + '); background-repeat:no-repeat; background-position:bottom center; overflow:hidden; z-index:10;'
	num_style = 'font-size:1.4em; padding-left:1em; font-weight:bold;';
	
	// top block
	top_block = '<div class="m95_top" style="' + top_block_style + '"></div>';

	// build num block
	if ( !isNaN(num) ) num_block = '<div class="m95_num" style="' + num_style + '">#' + num + '</div>';
	
	// build text block
	m95_text = '<div class="m95_text">' + text + '</div>';
	
	// build footer
	m95_footer = '<div style="margin:6px 0; text-align:left; font-family:Tahoma, Geneva, sans-serif; letter-spaceing:2px; font-size:10px; font-style:normal; font-weight:normal; line-height:1.2em;">';
	m95_footer += 'from the <a href="http://scratchpad.wikia.com/wiki/Myspace_95_Thesis" onclick="window.open(this.href,\'_blank\');return false;">myspace 95 thesis</a>';
	m95_footer += '<br />for more info, <a href="http://myspacecivilized.blogspot.com/2006/11/myspace-95-thesis-widget.html" onclick="window.open(this.href,\'_blank\');return false;" style="cursor:help;">click here</a></div>';

	// build mid block
	mid_block = '<div class="m95_mid" style="' + mid_block_style + '">' + num_block + m95_text + m95_footer + '</div>';
	
	// build yanker
	yanker = '<div class="m95_close" onclick="javascript:this.parentNode.style.display=\'none\';" style="position:absolute; width:90%; bottom:12px; text-align:right; padding-right:12px; font-style:normal; font-weight:normal; font-size:10px; cursor:pointer;"><a href="#">&laquo; yoink! &raquo;</a></div>';
	
	// assemble block
	html = '<div class="m95_wx" style="' + wx_style + '">' + top_block + mid_block + yanker + "</div>";

	
// *** RETURN

	return html;	
	
} // end Fx
/*____________________________________________________________________________*/


// js_get_script_path
/*____________________________________________________________________________*/
function js_get_script_path(js_fname)
{
 var _regex = js_fname;
 var _debug = 0;
 var _SCRIPTS = new Array();
 var script_path = ""; // return

 // Get Script Tag Array
 _SCRIPTS = document.getElementsByTagName('script');

 // Loop Search for m95 tag
 for ( i=0; i<_SCRIPTS.length; i++ )
 {
   var _src = _SCRIPTS[i].src
   if ( _debug ) alert(_src);
   if ( _src.search(_regex) != -1 )
   {
           if ( _debug ) alert('script_path [' + _src + '] found!');
           script_path = _src.substr(0, _src.lastIndexOf('/') + 1);
           break;
   }
 }

 if ( _debug ) alert('returning script_path [' + script_path + ']');
 return script_path;
}
/*____________________________________________________________________________*/


// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
// Source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Math:random
function js_rand(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }
